package com.example.werewolf.service.impl.role;

import com.example.werewolf.common.ActionType;
import com.example.werewolf.entity.Event;
import com.example.werewolf.entity.GamePhase;
import com.example.werewolf.entity.Player;
import com.example.werewolf.model.ActionSkill;
import com.example.werewolf.service.ActionStrategy;
import com.example.werewolf.utils.PrintUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class RallyActionStrategy implements ActionStrategy {
    @Override
    public ActionSkill execute(List<Player> players, List<Player> actionPlayers,
                               GamePhase phase, List<Event> publicEvents, SseEmitter emitter) throws IOException {
        List<Integer> rallyList = new ArrayList<>();
        List<Integer> unRallyList = new ArrayList<>();

        for (Player player : actionPlayers) {
            // 玩家异步上警
            boolean isRally = Math.random() > 0.3;
            if (isRally) {
                rallyList.add(player.getId());
            } else {
                unRallyList.add(player.getId());
                player.setSpeech(false);
            }

            // 上警 action
//            PrintUtils.printPlayer(emitter, player, action);
        }


        // 主持人汇总上警情况
        String rally = String.format("【主持人 + 存活玩家】：上警的玩家有%s, 不上警的玩家有%s", rallyList, unRallyList);
        publicEvents.add(Event.builder().description(rally).build());
        PrintUtils.printSystem(emitter, rally);

        return ActionSkill.builder()
                .actionType(ActionType.RALLY)
                .rallyContent(rally)
                .build();
    }
}
